<script setup>
import { reactive } from 'vue'
import { login } from '@/api/user.js'
import { useRouter } from 'vue-router'
const router = useRouter()
const formData = reactive({
  mobile: '13800000002',
  password: 'hm#qd@23!',
  isAgree: false
})

// 登录验证
const validateAgree = (rule, value, callback) => {
  value ? callback() : callback(new Error('必须同意才能登陆'))
}
// 校验通过
const onFinish = async (values) => {
  delete values.isAgree
  console.log(values)
  const res = await login(values)
  console.log(res)
  router.push('/home')
}
</script>

<template>
  <div class="login-container">
    <div class="logo">
      <div class="icon"></div>
      <!-- <img src="@/assets/login.png" width="300" alt="" /> -->
      <!-- <p>开箱即用的中后台管理系统</p> -->
    </div>
    <div class="form">
      <h3>iHRM 人力资源管理系统</h3>
      <a-card class="login-card">
        <a-form
          @finish="onFinish"
          :model="formData"
          :label-col="{ span: 8 }"
          :wrapper-col="{ span: 16 }"
          autocomplete="off"
        >
          <a-form-item
            label="手机号"
            name="mobile"
            :rules="[
              { required: true, message: '手机号不能为空', trigger: 'blur' },
              { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '手机号格式不正确', trigger: 'blur' }
            ]"
          >
            <a-input v-model:value="formData.mobile" />
          </a-form-item>

          <a-form-item
            label="密码"
            name="password"
            :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
          >
            <a-input-password v-model:value="formData.password" />
          </a-form-item>

          <a-form-item
            name="isAgree"
            :wrapper-col="{ offset: 8, span: 16 }"
            :rules="[{ validator: validateAgree, trigger: 'change' }]"
          >
            <a-checkbox v-model:checked="formData.isAgree">用户平台使用协议</a-checkbox>
          </a-form-item>

          <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button type="primary" html-type="submit">登录</a-button>
          </a-form-item>
        </a-form>
      </a-card>
    </div>
  </div>
</template>

<style lang="less">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/login.png) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      //   background: url(../../assets/common/logo.png) no-repeat 70px center / contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 50px;
    .ant-card {
      width: 320px;
      border: none;
      padding: 0;
    }
    h3 {
      padding-left: 30px;
      font-size: 24px;
    }
  }
}
</style>
